/**
 * Created by jocky on 2017/7/19.
 */
var traderObj = {
lists: function (type, direction) {
        //显示loading
        show_loading_table('trader_lists_table', 7);

        var params = {};
        params.isRender = 2;//ajax调用

        var pageNo_dom = 'trader_lists_pageNo';
        var pageTotal_dom = 'trader_lists_pageTotal';
        params.pageNo = getPageNo(pageNo_dom, pageTotal_dom, type, direction);

        $.each($('#trader_search_form').find('input,select'), function (i, v) {
            var name = $.trim($(v).attr('name'));
            if (name != '') {
                params[name] = $(v).val();
            }
        });

        $.getJSON('/trader/lists', params, function (json) {
            $("#" + pageNo_dom).val(parseInt(json.pageNo));
            $("#" + pageTotal_dom).html(parseInt(json.pageTotal));

            var tr = "";
            $.each(json.lists, function (i, v) {
                var statusStr;
                if (v.login_flag==1 && v.realNameStatus == 1) {
                    statusStr = "<i class='fa fa-check' style='color:green;'></i>&nbsp;&nbsp;已实名";
                } else if (v.login_flag==3 && v.realNameStatus == 0){
                    statusStr = "<i class='fa fa-spinner' style='color:#D3D3D3;'></i>&nbsp;&nbsp;待实名";
                }else if (v.login_flag==2 && v.realNameStatus == null){
                    statusStr = "<i class='fa fa-exclamation-circle' style='color:orange;'></i>&nbsp;&nbsp;新注册-未实名";
                }else{
                    statusStr = "<i class='fa fa-exclamation-triangle' style='color:B22222;'></i>&nbsp;&nbsp;拒绝实名";
                }
                tr += "<tr>";
                tr += "<td><a href='javascript:void(0)' title='查看 / 修改账户信息' onclick=\"traderObj.setForm(" + v.userID + ")\">" + v.loginName + "</a></td>";
                tr += "<td>" + v.accountName + "</td>";
                // tr += "<td class='text-center'>" + (v.version == 1 ? "迷你版" : "标准版") + "</td>";
                //                 // tr += "<td class='text-center'>" + (v.register_type == 1 ? "网页注册" : "手机注册") + "</td>";
                tr += "<td class='text-center'>" + v.RegisterDate+ "</td>";
                tr += "<td class='text-center'>" + statusStr + "</td>";
                // tr += "<td class='text-center'><a href='javascript:void(0)' onclick=\"templateObj.assignForm('" + v.loginName + "','" + v.accountName + "',2," + v.userID + ")\"> 查看 | 设置</a></td>";

                tr += "</tr>";
            });

            if (tr == "") {
                tr = "<tr><td colspan='7' class='text-center'>--</td></tr>";
            }

            $("#trader_lists_table").find('tbody').html(tr);

        })
    },

    /**
    添加用户
    **/
    addForm: function () {
        $.get('/trader/add-form', function (html) {
            $('#modal_pop_title').html('<i>添加交易客户</i>');
            $(document).off('click', '#modal_pop_btn');
            $(document).on('click', '#modal_pop_btn', function () {
                traderObj.add();
            });

            $('#modal_pop_body').html(html);
            $('#modal_pop').modal('show');
        })
    },

    /**
    添加用户信息
    **/
    add: function () {
        var params = {};
        var valid = true;
        $.each($("#trade_add_form").find("input,select"), function (i, v) {
            var name = $(v).attr('name');
            if (!name) return true;

            var type = $(v).attr('type');
            if (type == 'RADIO' || type == 'radio') {
                if ($(v).is(':checked')) {
                    params[name] = parseInt(v.value);
                }
            } else {
                params[name] = $(v).val();
                if (name != 'alias' && !params[name]) {
                    new PNotify({
                        title: '添加错误',
                        text: '必填项，不能未空！',
                        delay: 5000,
                        styling: 'bootstrap3'
                    });

                    $(v).focus();
                    valid = false;
                    return false;
                }
            }
        });

        if (!valid) return false;

        $.post('/trader/add-submit', params, function (json) {
            var obj = {
                title: '操作通知',
                text: json.msg,
                styling: 'bootstrap3',
                delay: 5000,
                type: ''
            };
            if (json.code == 1) {
                obj.type = 'info';
                obj.addclass = 'dark';

                $('#modal_pop').modal('hide');
                traderObj.lists(1, 1);
            } else {
                obj.type = 'error';
            }
            new PNotify(obj);

        }, 'json');
    },
    
    /**
     * 账户信息表单
     * @param uid
     */
    setForm: function (uid) {
        $.get('/trader/set-form', {userID:uid}, function (html) {
            $('#modal_pop2_title').html('<i>账户信息</i>');
            $('#modal_pop2_body').html(html);
            $('#modal_pop2').modal('show');
        })
    },

    /**
     * 修改别名
     * @param uid
     * @param obj
     */
    changeAlias: function (uid, obj) {
        var alias = $(obj).closest('div').find('input[name=alias]').val();
        $.getJSON('/trader/change-alias', {uid: uid, alias: alias}, function (json) {
            var obj = {
                title: '操作通知',
                text: json.msg,
                styling: 'bootstrap3',
                delay: 5000,
                type: ''
            };

            if (json.code == 1) {
                obj.type = 'info';
                obj.addclass = 'dark';
            } else {
                obj.type = 'error';
            }
            new PNotify(obj);
        })
    },

    /**
     * 修改密码
     * @param uid
     * @param obj
     */
    modifyPwd1: function (uid,obj) {

        var password = $(obj).closest('div').find('input[name=pwd]').val();

        $.getJSON('/trader/change-pwd1', {uid: uid, password: password}, function (json) {
            var obj = {
                title: '操作通知',
                text: json.msg,
                styling: 'bootstrap3',
                delay: 5000,
                type: ''
            };

            if (json.code == 1) {
                obj.type = 'info';
                obj.addclass = 'dark';
            } else {
                obj.type = 'error';
            }
            new PNotify(obj);
        })
    }
};